﻿
@{
    ViewData["Title"] = "添加角色";
    Layout = "_Operate";
}
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-role-add">
        <div class="form-group">
            <label class="col-sm-3 control-label ">角色名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="CName" id="CName" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">权限字符：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="EName" id="EName" required>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 控制器中定义的权限字符，如：@@RequiresRoles("")</span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">显示顺序：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="Sort" id="Sort" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">状态：</label>
            <div class="col-sm-8">
                <div class="radio-box">
                    <input type="radio" name="Status" value="1" checked="checked">
                    <label>启用</label>
                </div>
                <div class="radio-box">
                    <input type="radio" name="Status" value="0">
                    <label>停用</label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <input id="remark" name="Remark" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">菜单权限</label>
            <div class="col-sm-8">
                <div id="menuTrees" name="MenusId" class="ztree"></div>
            </div>
        </div>
    </form>
</div>

@section Scripts{
    <script src="~/ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js"></script>
    <script type="text/javascript">
        $(function () {
            var url = "/api/SysMenu/QueryMenuZtreeData";
            var options = {
                id: "menuTrees",
                url: url,
                check: { enable: true },
                expandLevel: 0
            };
            $.tree.init(options);
        });

        $("#form-role-add").validate({
            rules: {
                onkeyup: false,
                CName: {
                    remote: {
                        url: "/api/SysRole/CheckRoleCName",
                        type: "get",
                        dataType: "json",
                        dataFilter: function (data, type) {
                            return data == "false";
                        }
                    }
                },
                EName: {
                    remote: {
                        url: "/api/SysRole/CheckRoleEName",
                        type: "get",
                        dataType: "json",
                        dataFilter: function (data, type) {
                            return data == "false";
                        }
                    }
                },
                roleSort: {
                    digits: true
                },
            },
            messages: {
                "CName": {
                    remote: "角色名称已经存在"
                },
                "EName": {
                    remote: "角色权限已经存在"
                }
            },
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                add();
            }
        }

        function add() {
            var data = $.common.formToJSON("form-role-add");
            data["MenusId"] = $.tree.getCheckedNodes();
            data["Sort"] = parseInt(data["Sort"]);
            $.operate.saveTab("/api/SysRole/AddSave", JSON.stringify(data));
        }
    </script>
}
